package com.j48.entrancegurad.controller;

import com.j48.entrancegurad.model.Admin;
import com.j48.entrancegurad.model.Host;
import com.j48.entrancegurad.model.User;
import com.j48.entrancegurad.model.Visitor;
import com.j48.entrancegurad.service.IAdminService;
import com.j48.entrancegurad.service.IHostService;
import com.j48.entrancegurad.service.IUserService;
import com.j48.entrancegurad.service.IVisitorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Repository;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.swing.plaf.ViewportUI;
import java.sql.Struct;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
public class VisitorController {
    @Autowired
    private IVisitorService visitorService;

    @Autowired
    private IUserService userService;

    @Autowired
    private IAdminService adminService;

    @Autowired
    private IHostService hostService;
    /**
     * 未到访访客列表
     * @param request
     * @return
     */
    @RequestMapping("/visitorListAble")
    @ResponseBody
    public ModelAndView visitorListAble(HttpServletRequest request){
        ModelAndView model=null;
        List<Visitor> list = new ArrayList<Visitor>();
        list=visitorService.selectAllAble();
        List<User> userlist = new ArrayList<User>();
        List<Host> hostList=new ArrayList<Host>();
        if(list!=null) {
            List<Integer> user_idList = new ArrayList<Integer>();
            for (Visitor v : list) {
                user_idList.add(v.getVisitor_user_id());
            }
            for (int i : user_idList) {
                User user = new User();
                user.setUser_id(i);
                user = userService.selectByUser_id(user);
                userlist.add(user);
            }
            for (User u:userlist) {
                Host host=new Host();
                host.setHost_id(u.getUser_host_id());
                host=hostService.selectById(host);
                hostList.add(host);
            }
        }
        request.getSession().setAttribute("visitorList", list);
        request.getSession().setAttribute("visitorUserList", userlist);
        request.getSession().setAttribute("visitorHostList",hostList);
        model = new ModelAndView("jsp/visitorList");
        return model;
    }

    /**
     * 所有访客列表
     * @param request
     * @return
     */
    @RequestMapping("/visitorList")
    @ResponseBody
    public ModelAndView visitorList(HttpServletRequest request){
        ModelAndView model=null;
        List<Visitor> list = new ArrayList<Visitor>();
        list=visitorService.selectAll();
        List<User> userlist = new ArrayList<User>();
        List<Admin> adminList=new ArrayList<Admin>();
        List<Host> hostList=new ArrayList<Host>();
        if(list!=null) {
            List<Integer> user_idList = new ArrayList<Integer>();
            List<Integer> admin_idList=new ArrayList<Integer>();
            for (Visitor v : list) {
                user_idList.add(v.getVisitor_user_id());
                admin_idList.add(v.getVisitor_admin_id());
            }
            for (int i : user_idList) {
                User user = new User();
                user.setUser_id(i);
                user = userService.selectByUser_id(user);
                userlist.add(user);
            }
            for (User u:userlist) {
                Host host=new Host();
                host.setHost_id(u.getUser_host_id());
                host=hostService.selectById(host);
                hostList.add(host);
            }
            for (int i:admin_idList) {
                Admin admin=new Admin();
                admin.setAdmin_id(i);
                admin=adminService.selectById(admin);
                adminList.add(admin);
            }
        }
        request.getSession().setAttribute("visitorAllList", list);
        request.getSession().setAttribute("visitorAllUserList", userlist);
        request.getSession().setAttribute("visitorAllAdminList",adminList);
        request.getSession().setAttribute("visitorAllHostList",hostList);
        model = new ModelAndView("jsp/visitorListAll");
        return model;
    }

    /**
     * 按访客姓名查询未到访访客信息
     * @param visitor_name 访客姓名
     * @param request
     * @return
     */
    @RequestMapping("/visitorListByVisitor")
    @ResponseBody
    public ModelAndView visitorListByVisitor(String visitor_name,HttpServletRequest request){
        if(null==visitor_name||"".equals(visitor_name)){
            return visitorListAble(request);
        }else{
            ModelAndView model = null;
            Visitor visitor=new Visitor();
            visitor.setVisitor_name(visitor_name);
            List<Visitor> list = new ArrayList<Visitor>();
            list = visitorService.selectByVisitor(visitor);
            List<User> userlist = new ArrayList<User>();
            List<Host> hostList=new ArrayList<Host>();
            if(list!=null) {
                List<Integer> user_idList = new ArrayList<Integer>();
                for (Visitor v : list) {
                    user_idList.add(v.getVisitor_user_id());
                }
                for (int i : user_idList) {
                    User user = new User();
                    user.setUser_id(i);
                    user = userService.selectByUser_id(user);
                    userlist.add(user);
                }
                for (User u:userlist) {
                    Host host=new Host();
                    host.setHost_id(u.getUser_host_id());
                    host=hostService.selectById(host);
                    hostList.add(host);
                }
            }
            request.getSession().setAttribute("visitorList", list);
            request.getSession().setAttribute("visitorUserList", userlist);
            request.getSession().setAttribute("visitorHostList",hostList);
            model = new ModelAndView("jsp/visitorList");
            return model;
        }
    }

    /**
     * 按访客姓名查询所有访客信息
     * @param visitor_name 访客姓名
     * @param request
     * @return
     */
    @RequestMapping("/visitorListAllByVisitor")
    @ResponseBody
    public ModelAndView visitorListAllByVisitor(String visitor_name,HttpServletRequest request){
        if(null==visitor_name||"".equals(visitor_name)){
            return visitorList(request);
        }else{
            ModelAndView model = null;
            Visitor visitor=new Visitor();
            visitor.setVisitor_name(visitor_name);
            List<Visitor> list = new ArrayList<Visitor>();
            list = visitorService.selectAllByVisitor(visitor);
            List<User> userlist = new ArrayList<User>();
            List<Admin> adminList=new ArrayList<Admin>();
            List<Host> hostList=new ArrayList<Host>();
            if(list!=null) {
                List<Integer> user_idList = new ArrayList<Integer>();
                List<Integer> admin_idList=new ArrayList<Integer>();
                for (Visitor v : list) {
                    user_idList.add(v.getVisitor_user_id());
                    admin_idList.add(v.getVisitor_admin_id());
                }
                for (int i : user_idList) {
                    User user = new User();
                    user.setUser_id(i);
                    user = userService.selectByUser_id(user);
                    userlist.add(user);
                }
                for (User u:userlist) {
                    Host host=new Host();
                    host.setHost_id(u.getUser_host_id());
                    host=hostService.selectById(host);
                    hostList.add(host);
                }
                for (int i:admin_idList) {
                    Admin admin=new Admin();
                    admin.setAdmin_id(i);
                    admin=adminService.selectById(admin);
                    adminList.add(admin);
                }
            }
            request.getSession().setAttribute("visitorAllList", list);
            request.getSession().setAttribute("visitorAllUserList", userlist);
            request.getSession().setAttribute("visitorAllAdminList",adminList);
            request.getSession().setAttribute("visitorAllHostList",hostList);
            model = new ModelAndView("jsp/visitorListAll");
            return model;
        }
    }

    /**
     * 按用户查询未到访访客信息
     * @param visitor_user_name 用户姓名
     * @param request
     * @return
     */
    @RequestMapping("/visitorListByUser")
    @ResponseBody
    public ModelAndView visitorListByUser(String visitor_user_name,HttpServletRequest request){
        if(null==visitor_user_name||"".equals(visitor_user_name)){
            return visitorListAble(request);
        }else{
            ModelAndView model = null;
            User user1 = new User();
            user1.setUser_name(visitor_user_name);
            List<User> userlist = userService.selectByUser_name(user1);
            List<User> userlist1=new ArrayList<User>();
            List<Visitor> list = new ArrayList<Visitor>();
            List<Host> hostList=new ArrayList<Host>();
            List<Visitor> visitorList=new ArrayList<Visitor>();
            if(userlist!=null) {
                for (User user: userlist) {
                Visitor visitor = new Visitor();
                visitor.setVisitor_user_id(user.getUser_id());
                list = visitorService.selectByUser(visitor);
                if(list!=null){
                    for (Visitor v:list) {
                        visitorList.add(v);
                    }
                    List<Integer> user_idList = new ArrayList<Integer>();
                    for (Visitor v : list) {
                        user_idList.add(v.getVisitor_user_id());
                    }
                    for (int i : user_idList) {
                        User user2 = new User();
                        user2.setUser_id(i);
                        user2 = userService.selectByUser_id(user2);
                        userlist1.add(user2);
                    }

                }
                }
                for (User u:userlist1) {
                    Host host=new Host();
                    host.setHost_id(u.getUser_host_id());
                    host=hostService.selectById(host);
                    hostList.add(host);
                }
            }
            model = new ModelAndView("jsp/visitorSearchByUser");
            request.getSession().setAttribute("visitorList", visitorList);
            request.getSession().setAttribute("visitorUser", userlist1);
            request.getSession().setAttribute("visitorHost", hostList);
            return model;
        }
    }

    /**
     * 按用户查询所有访客信息
     * @param visitor_user_name 查询用户姓名
     * @param request
     * @return
     */
    @RequestMapping("/visitorListAllByUser")
    @ResponseBody
    public ModelAndView visitorListAllByUser(String visitor_user_name,HttpServletRequest request){
        if(null==visitor_user_name||"".equals(visitor_user_name)){
            return visitorList(request);
        }else{
            ModelAndView model = null;
            User user1 = new User();
            user1.setUser_name(visitor_user_name);
            List<User> userlist = userService.selectByUser_name(user1);
            List<User> userlist1=new ArrayList<User>();
            List<Visitor> list = new ArrayList<Visitor>();
            List<Visitor> visitorList=new ArrayList<Visitor>();
            List<Admin> adminList=new ArrayList<Admin>();
            List<Host> hostList=new ArrayList<Host>();
            if(userlist!=null) {
                for (User user: userlist) {
                    Visitor visitor = new Visitor();
                    visitor.setVisitor_user_id(user.getUser_id());
                    list = visitorService.selectAllByUser(visitor);
                    if(list!=null){
                        List<Integer> user_idList = new ArrayList<Integer>();
                        List<Integer> admin_idList=new ArrayList<Integer>();
                        for (Visitor v:list) {
                            visitorList.add(v);
                            user_idList.add(v.getVisitor_user_id());
                            admin_idList.add(v.getVisitor_admin_id());
                        }
                        for (int i : user_idList) {
                            User user2 = new User();
                            user2.setUser_id(i);
                            user2 = userService.selectByUser_id(user2);
                            userlist1.add(user2);
                        }

                        for (int i:admin_idList) {
                            Admin admin=new Admin();
                            admin.setAdmin_id(i);
                            admin=adminService.selectById(admin);
                            adminList.add(admin);
                        }
                    }
                }
                for (User u:userlist1) {
                    Host host=new Host();
                    host.setHost_id(u.getUser_host_id());
                    host=hostService.selectById(host);
                    hostList.add(host);
                }
            }
            model = new ModelAndView("jsp/visitorAllSearchByUser");
            request.getSession().setAttribute("visitorAllList", visitorList);
            request.getSession().setAttribute("visitorAllUser", userlist1);
            request.getSession().setAttribute("visitorAllHost",hostList);
            request.getSession().setAttribute("visitorAllAdminList",adminList);
            return model;
        }
    }

    /**
     * 按时间查询访客信息
     * @param visitor_arrivaltime 到访日期
     * @param request
     * @return
     */
    @RequestMapping("/visitorListAllByDate")
    @ResponseBody
    public ModelAndView visitorListByDate(String visitor_arrivaltime, HttpServletRequest request){
        if(null==visitor_arrivaltime||"".equals(visitor_arrivaltime)){
            return visitorList(request);
        }else {
            ModelAndView model = null;
            List<Visitor> list = new ArrayList<Visitor>();

            Timestamp ts = null;
            visitor_arrivaltime += " 00:00:00";

            try {
                ts = Timestamp.valueOf(visitor_arrivaltime);
            } catch (Exception e) {
                e.printStackTrace();
            }
            Visitor visitor=new Visitor();
            visitor.setVisitor_arrivaltime(ts);
            list = visitorService.selectByDate(visitor);
            List<User> userlist = new ArrayList<User>();
            List<Admin> adminList=new ArrayList<Admin>();
            List<Host> hostList=new ArrayList<Host>();
            if(list!=null) {
                List<Integer> user_idList = new ArrayList<Integer>();
                List<Integer> admin_idList=new ArrayList<Integer>();
                for (Visitor v : list) {
                    user_idList.add(v.getVisitor_user_id());
                    admin_idList.add(v.getVisitor_admin_id());
                }
                for (int i : user_idList) {
                    User user = new User();
                    user.setUser_id(i);
                    user = userService.selectByUser_id(user);
                    userlist.add(user);
                }
                for (User u:userlist) {
                    Host host=new Host();
                    host.setHost_id(u.getUser_host_id());
                    host=hostService.selectById(host);
                    hostList.add(host);
                }
                for (int i:admin_idList) {
                    Admin admin=new Admin();
                    admin.setAdmin_id(i);
                    admin=adminService.selectById(admin);
                    adminList.add(admin);
                }
            }
            model = new ModelAndView("jsp/visitorListAll");
            request.getSession().setAttribute("visitorAllList", list);
            request.getSession().setAttribute("visitorAllUserList", userlist);
            request.getSession().setAttribute("visitorAllHostList",hostList);
            request.getSession().setAttribute("visitorAllAdminList",adminList);
            return model;
        }
    }

    /**
     * 添加访客
     * @param request
     * @return
     */
    @RequestMapping("/addVisitor")
    @ResponseBody
    public String visitorAdd(HttpServletRequest request){
        String visitor_name=request.getParameter("visitor_name");
        String visitor_number=request.getParameter("visitor_number");
        int visitor_number1;
        try {
            visitor_number1=Integer.parseInt(visitor_number);
        }catch (Exception e){
            return "false";
        }
        String visitor_user_name=request.getParameter("visitor_user_name");
        String visitor_user_building=request.getParameter("visitor_user_building");
        int visitor_user_building1;
        try {
            visitor_user_building1=Integer.parseInt(visitor_user_building);
        }catch (Exception e){
            return "false";
        }
        String visitor_user_unit=request.getParameter("visitor_user_unit");
        int visitor_user_unit1;
        try {
            visitor_user_unit1=Integer.parseInt(visitor_user_unit);
        }catch (Exception e){
            return "false";
        }
        String visitor_user_housenumber=request.getParameter("visitor_user_housenumber");
        int visitor_user_housenunber1;
        try {
            visitor_user_housenunber1=Integer.parseInt(visitor_user_housenumber);
        }catch (Exception e){
            return "false";
        }
        String visitor_time_ymd=request.getParameter("visitor_time_ymd");
        String visitor_time_hms=request.getParameter("visitor_time_hms");
        if(null==visitor_name||"".equals(visitor_name)||null==visitor_number||"".equals(visitor_number)||null==visitor_user_name||"".equals(visitor_user_name)||null==visitor_user_building||"".equals(visitor_user_building)||null==visitor_user_unit||"".equals(visitor_user_unit)||null==visitor_user_housenumber||"".equals(visitor_user_housenumber)||null==visitor_time_ymd||"".equals(visitor_time_ymd)||null==visitor_time_hms||"".equals(visitor_time_hms)){
            return "false";
        }else {
            User user=new User();
            user.setUser_name(visitor_user_name);
            List<User> userList=userService.selectByUser_name(user);
            if(userList!=null){
                for (User u:userList) {
                    Host host=new Host();
                    host.setHost_id(u.getUser_host_id());
                    host=hostService.selectById(host);
                    if(host.getHost_building()==visitor_user_building1&&host.getHost_unit()==visitor_user_unit1&&host.getHost_housenumber()==visitor_user_housenunber1){
                        Visitor visitor=new Visitor();
                        visitor.setVisitor_name(visitor_name);
                        visitor.setVisitor_number(visitor_number1);
                        visitor.setVisitor_user_id(u.getUser_id());
                        Timestamp ts1 = new Timestamp(new Date().getTime());
                        visitor.setVisitor_createtime(ts1);
                        Timestamp ts = null;
                        String visitor_time = visitor_time_ymd + " " + visitor_time_hms;
                        try {
                            ts = Timestamp.valueOf(visitor_time);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        visitor.setVisitor_time(ts);
                        boolean b=visitorService.insert(visitor);
                        if(b){
                            flush(request);
                            return "true";
                        }
                    }
                }
            }
        }
        return "false";
    }
    /**
     * 确认到访
     * @param req
     * @return
     */
    @RequestMapping("/confirmVisitor")
    @ResponseBody
    public String confirmVisitor(HttpServletRequest req){
        if(req.getParameter("visitor_id")!=null){
            int visitor_id;
            try {
                 visitor_id= Integer.parseInt(req.getParameter("visitor_id"));
            }catch (Exception e){
                return "false";
            }
        Visitor visitor=new Visitor();
        visitor.setVisitor_id(visitor_id);
        Admin admin=(Admin)req.getSession().getAttribute("admin");
        visitor.setVisitor_admin_id(admin.getAdmin_id());
        Timestamp timestamp=new Timestamp(new Date().getTime());
        visitor.setVisitor_arrivaltime(timestamp);
        if(visitorService.update(visitor)){
            return "true";
        }
        }
        return "false";
    }
    /**
     * 删除访客
     * @param req
     * @return
     */
    @RequestMapping("/deleteVisitor")
    @ResponseBody
    public String deleteVisitor(HttpServletRequest req){
        int visitor_id=Integer.parseInt(req.getParameter("visitor_id"));
        Visitor visitor=new Visitor();
        visitor.setVisitor_id(visitor_id);
        if(visitorService.delete(visitor)){
            return "true";
        }
        return "false";
    }
    /**
     * 刷新未到访访客表及所有访客表
     * @param req
     * @return
     */
    @RequestMapping("/visitorflush")
    @ResponseBody
    public String visitorflush(HttpServletRequest req){
        flush(req);
        return "true";
    }

    /**
     * 刷新未到访访客表及所有访客表
     * @param req
     */
    public void flush(HttpServletRequest req){
        req.getSession().removeAttribute("visitorList");
        req.getSession().removeAttribute("visitorUserList");
        req.getSession().removeAttribute("visitorListAll");
        req.getSession().removeAttribute("visitorUserListAll");
        List<Visitor> visitorList = new ArrayList<Visitor>();
        visitorList=visitorService.selectAllAble();
        List<User> visitorUserList = new ArrayList<User>();
        List<Host> hostList=new ArrayList<Host>();
        if(visitorList!=null) {
            List<Integer> visitorUser_idList = new ArrayList<Integer>();
            for (Visitor v : visitorList) {
                visitorUser_idList.add(v.getVisitor_user_id());
            }
            for (int i : visitorUser_idList) {
                User user = new User();
                user.setUser_id(i);
                user = userService.selectByUser_id(user);
                visitorUserList.add(user);
            }
            for (User u:visitorUserList) {
                Host host=new Host();
                host.setHost_id(u.getUser_host_id());
                host=hostService.selectById(host);
                hostList.add(host);
            }
        }
        req.getSession().setAttribute("visitorList", visitorList);
        req.getSession().setAttribute("visitorUserList", visitorUserList);
        req.getSession().setAttribute("visitorHostList",hostList);
        List<Visitor> visitorListAll = new ArrayList<Visitor>();
        visitorListAll=visitorService.selectAll();
        List<User> visitorUserListAll = new ArrayList<User>();
        List<Host> hostListAll=new ArrayList<Host>();
        if(visitorListAll!=null) {
            List<Integer> visitorUser_idListAll = new ArrayList<Integer>();
            for (Visitor v : visitorListAll) {
                visitorUser_idListAll.add(v.getVisitor_user_id());
            }
            for (int i : visitorUser_idListAll) {
                User user = new User();
                user.setUser_id(i);
                user = userService.selectByUser_id(user);
                visitorUserListAll.add(user);
            }
            for (User u:visitorUserListAll) {
                Host host=new Host();
                host.setHost_id(u.getUser_host_id());
                host=hostService.selectById(host);
                hostListAll.add(host);
            }
        }
        req.getSession().setAttribute("visitorAllList", visitorListAll);
        req.getSession().setAttribute("visitorAllUserList", visitorUserListAll);
        req.getSession().setAttribute("visitorAllHostList",hostListAll);
    }
}
